package com.xiaorong.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaorong.Principal;
import com.xiaorong.dao.AdminDao;
import com.xiaorong.dao.RoleDao;
import com.xiaorong.entity.Admin;
import com.xiaorong.entity.Role;

/**
 * 账号设置
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{
	
	@Resource
	private AdminDao adminDao;
	@Resource
	private RoleDao roleDao;
	
	@RequestMapping(value = "/setting", method = RequestMethod.GET)
	public String setting(HttpServletRequest request, HttpSession session) {
		Principal principal = (Principal) session.getAttribute("adminUsername");
		Admin admin = adminDao.find(principal.getId());
		request.setAttribute("admin", admin);
		return "/admin/setting";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request) {
		request.setAttribute("admins", adminDao.findList(null, null, null, null));
		return "/admin/list";
	}
	
	@RequestMapping(value = "/check_username", method = RequestMethod.GET)
	public @ResponseBody
	boolean checkUsername(String username) {
		if (StringUtils.isEmpty(username)) {
			return false;
		}
		if (adminDao.usernameExists(username)) {
			return false;
		} else {
			return true;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/changePass", method = RequestMethod.POST)
	public Map<String, Object>  changePass(Admin admin,  String newPassword, HttpSession session) {
		Map<String, Object> data = new HashMap<String,Object>();
		boolean result = false;
		if(StringUtils.isNotBlank(admin.getPassword())){
			Admin target = adminDao.find(admin.getId());
			
			if(!StringUtils.isBlank(newPassword) && target.getPassword().equals(DigestUtils.md5Hex(admin.getPassword()))){
				Principal principal = (Principal) session.getAttribute("adminUsername");
				
				
				if(target.getId().equals(principal.getId())){
					
					target.setPassword(DigestUtils.md5Hex(newPassword));
					adminDao.merge(target);
					
					result = true;
				}
			}
		}
		
		if(result){
			data.put("result", true);
			data.put("reason", SUCCESS_MESSAGE);
		}else{
			data.put("result", false);
			data.put("reason", ERROR_MESSAGE);
		}
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public boolean update(Admin admin, Long[] roleIds, HttpSession session) {
		if(admin.getId() == null){
			return false;
		}
		Admin target = adminDao.find(admin.getId());
		
		target.setRoles(new HashSet<Role>(roleDao.findList(roleIds)));
		
		if(StringUtils.isNotBlank(admin.getPassword())){
			target.setPassword(DigestUtils.md5Hex(admin.getPassword()));
		}
		target.setName(admin.getName());
		
		adminDao.merge(target);
		return true;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpSession session) {
		if(id != null){
			Admin admin = adminDao.find(id);
			request.setAttribute("admin", admin);
			
			List<Long> roleIds = new ArrayList<Long>();
			for(Role r : admin.getRoles()){
				roleIds.add(r.getId());
			}
			request.setAttribute("roleIds", roleIds);
		}
		
		request.setAttribute("roles", roleDao.findList(null, null, null, null));
		return "/admin/edit";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	boolean save(Admin admin, Long[] roleIds) {
		if (adminDao.usernameExists(admin.getName())) {
			return false;
		}
		if(StringUtils.isBlank(admin.getPassword())){
			return false;
		}
		admin.setRoles(new HashSet<Role>(roleDao.findList(roleIds)));
		admin.setPassword(DigestUtils.md5Hex(admin.getPassword()));
		adminDao.persist(admin);
		
		return true;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete.do", method = RequestMethod.GET)
	public boolean delete(Long[] ids) {
		if(!ArrayUtils.isEmpty(ids)){
			for(Long id : ids){
				if(adminDao.find(id).getIsSuper()){
					continue;
				}
				adminDao.remove(id);
			}
		}
		return true;
	}
}
